class monkeyOrientation {
    constructor(options) {

        const _self = this;

        this.options = {
            enable: true,
            background: "#333",
            animation: true,
            text: "为了更好的体验，请使用竖屏浏览",
        };

        for (const i in options) {
            this.options[i] = options[i];
        }

        this.el = `
                <div class="mod-orient-layer__content">
                    <i class="icon mod-orient-layer__icon-orient"></i>
                    <div class="mod-orient-layer__desc">${this.options.text}</div>
                </div>
        `;

        this.style = `
    .mod-orient-layer {
        display: none;
        position: fixed;
        height: 100%;
        width: 100%;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        background: ${this.options.background};
        z-index: 9997;
    }

    .mod-orient-layer__content {
        position: absolute;
        width: 100%;
        top: 45%;
        margin-top: -75px;
        text-align: center;
    }

    .mod-orient-layer__icon-orient {
        background: url('') no-repeat;
        display: inline-block;
        width: 67px;
        height: 109px;
        transform: rotate(90deg);
        -webkit-transform: rotate(90deg);
        -webkit-animation: rotation infinite 1.5s ease-in-out;
        animation: rotation infinite 1.5s ease-in-out;
        background-size: 67px;
    }
    .mod-orient-layer__desc {
        margin-top: 20px;
        font-size: 15px;
        color: #fff;
    }
`;

        if (this.options.animation) {
            this.style = this.style + `    @-webkit-keyframes rotation {
        from {
            transform: rotate(90deg);
            -webkit-transform: rotate(90deg);
        }
        50% {
            transform: rotate(0deg);
            -webkit-transform: rotate(0deg);
        }
        to {
            transform: rotate(90deg);
            -webkit-transform: rotate(90deg);
        }
    }

    @keyframes rotation {
        from {
            transform: rotate(90deg);
            -webkit-transform: rotate(90deg);
        }
        50% {
            transform: rotate(0deg);
            -webkit-transform: rotate(0deg);
        }
        to {
            transform: rotate(90deg);
            -webkit-transform: rotate(90deg);
        }
    }`;
        }

        if (!this.options.enable) return;


        if (document.addEventListener) {
            document.addEventListener('DOMContentLoaded', function () {
                _self.init();
            }, false);
        } else {
            setTimeout(function () {
                _self.init();
            }, 1000 / 60);
        }

    }

    init() {

        const _self = this;
        const style = document.createElement('style');
        style.innerHTML = _self.style;
        document.head.appendChild(style);
        const layer = document.createElement("div");
        layer.className = 'mod-orient-layer';
        layer.innerHTML = this.el;
        document.body.appendChild(layer);
        const orientation = window.orientation,
            orientLayer = document.querySelector(".mod-orient-layer");

        if (orientation == 90 || orientation == -90) {
            orientLayer.style.display = "block";
        }

        window.onorientationchange = function (v) {
            _self.watch(orientLayer);
        };

        if (typeof module != 'undefined' && module.exports) {
            module.exports = monkeyOrientation;
        }
        else if (typeof define == 'function' && define.amd) {
            define(function () {
                return monkeyOrientation;
            });
        }
        else {
            window.monkeyOrientation = monkeyOrientation;
        }

    }

    watch(el) {
        if (orientation == 90 || orientation == -90) {
            el.style.display = "block";
        } else {
            el.style.display = "none";
        }
        if (this.options.callback) {
            this.options.callback(orientation);
        }
    }
}